<?php
   namespace app\admin\controller;
   use \think\Controller;
   use \think\Db;
   use \think\Cookie;
   use \think\Log;
   class Index extends Controller{

      public function index(){
           return $this->fetch('index/dl');
	 }

	 function login(){
         $cap=input('post.');
         $cat=new \think\captcha\Captcha;
         if($cat->check($cap['yzm'])){
         	$where=[
                  'admin'=>$cap['admin'],
                  'pass'=>$cap['pass']
         	 ];
             $res=Db::name('admin')->where($where)->select();
             if ($res) {
             	Cookie::set('admin',$cap['admin']);
                 echo JSON_encode(['status'=>'ok']);
             }else{
             	echo JSON_encode(['status'=>'fail','error'=>'账号或密码！']);
             }
         }else{
           echo JSON_encode(['status'=>'fail','error'=>'验证码错误！']);

         }

	 }


	 function user_add(){
	     return $this->fetch('index/user_add');
	 }


	 function pass(){
		 $p=input('post.');
	     $old_pas=Db::name('admin')->where('admin',Cookie::get('admin'))->find();
		 if($p['old_pas']!=$old_pas['pass']){echo JSON_encode(['status'=>'fail','error'=>'旧密码错误！']);die();}
		 $new_pas=Db::name('admin')->where('id',$old_pas['id'])->update(['pass'=>$p['new_pas']]);
		 if($new_pas){
			 Cookie::set('admin','');
		    echo JSON_encode(['status'=>'ok']);
		 }else{
		    echo JSON_encode(['status'=>'fail','error'=>'系统繁忙请稍后再试！']); 
		 }
	 }

	 function get_admin(){
        if(Cookie::has('admin')){
             echo JSON_encode(['status'=>'ok','admin'=>Cookie::get('admin')]);
        }else{
        	 echo JSON_encode(['status'=>'fail','error'=>'未登入！']);
        }
	 }

	  function out(){
      	  Cookie::set('admin','');
      }
   }
?>